/*
 * StockView.java
 * 
 * Copyright (c) 2012 Supin P Surendran <supinps@gmail.com>.
 * 
 * This file is part of InventoryMan.
 * 
 * InventoryMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * InventoryMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with InventoryMan.  If not, see <http ://www.gnu.org/licenses/>.
 */

/*
 * StockView.java
 *
 * Created on 24 Dec, 2012, 12:52:13 AM
 */

package InventoryMan;

import java.sql.SQLException;

/**
 *
 * @author Supin P Surendran <supinps@gmail.com>
 */
public class StockView extends javax.swing.JInternalFrame {

    /** Creates new form StockView
     * @param conn Connection to the Database
     */
    public StockView(java.sql.Connection conn) {
        imConnection=conn;
        tableItem=new javax.swing.table.DefaultTableModel(new Object[]{"Code","Item","Stock"},0);
        initComponents();
        TblItem.setModel(tableItem);
        com.ezware.oxbow.swingbits.table.filter.TableRowFilterSupport.forTable(TblItem).searchable(true).apply();
        initTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrllPane = new javax.swing.JScrollPane();
        TblItem = new javax.swing.JTable();
        BnExit = new javax.swing.JButton();

        setTitle("View Stock");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/icon_small.png"))); // NOI18N

        TblItem.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        scrllPane.setViewportView(TblItem);

        BnExit.setText("Exit");
        BnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BnExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(BnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(scrllPane, javax.swing.GroupLayout.DEFAULT_SIZE, 898, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrllPane, javax.swing.GroupLayout.PREFERRED_SIZE, 462, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BnExit)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BnExitActionPerformed
        this.dispose();
    }//GEN-LAST:event_BnExitActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BnExit;
    private javax.swing.JTable TblItem;
    private javax.swing.JScrollPane scrllPane;
    // End of variables declaration//GEN-END:variables

    //Manually Declared Variables
    java.sql.Connection imConnection;
    java.sql.Statement stmt;
    java.sql.ResultSet reply;
    org.jdesktop.swingx.autocomplete.AutoCompleteDecorator ac;
    javax.swing.table.DefaultTableModel tableItem;
    //End of Declaration
    private void initTable() {
        try {
            stmt = imConnection.createStatement();
            reply = stmt.executeQuery("SELECT code,name,stock FROM item");
            int code;
            String name;
            int stock;
            while(reply.next()) {
                code=reply.getInt("code");
                name=reply.getString("name");
                stock=reply.getInt("stock");
                tableItem.addRow(new Object[]{code,name,stock});
            }
        } catch (SQLException ex) {
            javax.swing.JOptionPane.showMessageDialog(this, ex.toString());
        }

    }

}
